package com.china.lottery.listener.task.service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.china.lottery.listener.dao.DiaryInfoDao;
import com.china.lottery.listener.dao.DictionaryDao;
import com.china.lottery.listener.dao.entity.LChannelInfo;
import com.china.lottery.listener.task.core.BaseNoteService;
import com.china.lottery.listener.util.Constant;
import com.china.lottery.listener.util.HttpClient3;
import com.china.lottery.listener.util.Tools;

/**
 * 渠道余额Service
 * 
 * @date 2015年4月8号
 * @author ch
 */
@Component("bbcBanlanceSynService")
public class BbcBanlanceService extends BaseNoteService {

	private static String NUMCODE = Constant.BBCCHANNELBANLANCE;

	@Autowired
	private DictionaryDao dicDao;

	@Autowired
	private DiaryInfoDao diaryInfoDao;
	
	public static final String wuCaiUrl=Constant.wuCaiUrl+"lottery!queryBalance";
	
	public static final double money = Constant.BANLANCECHANNEL;

	@Override
	public List<String> getNumbers() {

		return this.getNumberFromParent(dicDao.findByParentCode(NUMCODE));
	}

	@Override
	public String getContent() {
		StringBuffer sb = new StringBuffer();
		try {
			List<LChannelInfo> channel = diaryInfoDao.findAllChannel();
			if (!Tools.isBlank(channel)) {
				HttpClient3 http = new HttpClient3();
				for (LChannelInfo channelarr : channel) {
					String channelname = channelarr.getChannelName();
					String d = 	new SimpleDateFormat("MM-dd HH:mm").format(new Date());
					if(channelarr.getChannelCode().equals("100010")){
						String channelBanlance = diaryInfoDao.findAllChannelBanlance(channelarr.getChannelCode());
						if(!Tools.isBlank(channelBanlance)){
							double banlance=Double.valueOf(channelBanlance)/100;
							if(banlance<money){
								sb.append("渠道商:"+channelarr.getChannelName()+"余额: ");  
								channelBanlance=BigDecimal.valueOf(Long.valueOf(channelBanlance)).divide(new BigDecimal(100)).toString();
								sb.append(channelBanlance+ "元"+d).append(";");
							}
						}else {
							sb.append("渠道商余额: ");
							sb.append(channelname + ":" +"余额为空"+d).append(";");
						}	
					}else if (channelarr.getChannelCode().equals("100011")) {
						String banlance="0";
						try {
							banlance = http.doPost(wuCaiUrl).replace("\"", "");
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						double channelBanlance=Double.valueOf(banlance);
						if(channelBanlance<money){
							sb.append("渠道商:"+channelarr.getChannelName()+"余额: ");  
							sb.append(channelBanlance+ "元"+d).append(";");
						}
					}
					
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.info("渠道余额预警出错!!!!!!!"+e.getMessage());
		}
		return sb.toString();
	} 

}
